package com.mms.service.impl;

import com.mms.entity.model.JsonData;
import com.mms.entity.model.PagerTool;
import com.mms.entity.service.impl.EntityServiceImpl;
import com.mms.pojo.SysOperate;
import com.mms.pojo.SysRoleOperate;
import com.mms.service.SysOperateService;
import com.mms.utils.RequestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by dangz on 2017-5-22.
 */
@Service
public class SysOperateServiceImpl extends EntityServiceImpl<SysOperate> implements SysOperateService {
    @Override
    public SysOperate getOperateInPermission(SysRoleOperate operateid,Long accountid) {
        String hql = "select a from SysOperate a join a.sysRoles b join b.sysManagers c where a.id.menu=? and a.id.op=? and c.id=?";
        return (SysOperate) getObject(hql, operateid.getMenu(), operateid.getOp(), accountid);
    }

    @Override
    public List<SysOperate> getOperatesInPermissionByMenu(Long menuid, Long accountid) {
        String hql = "select a from SysOperate a join a.sysRoles b join b.sysManagers c where a.id.menu=? and c.id=? and a.isShow = 1 order by a.orderNo";
        return find(hql, menuid, accountid);
    }

    @Override
    public boolean checkPermission(SysRoleOperate operateid, Long accountid) {
        String hql = "select a from SysOperate a join a.sysRoles b join b.sysManagers c where a.id.menu=? and a.id.op=? and c.id=?";
        SysOperate operate = (SysOperate) getObject(hql, operateid.getMenu(),
                operateid.getOp(), accountid);
        return operate != null;
    }

    @Override
    public List<SysOperate> getOperatesInPermission(Long menuid, String role) {
        String hql = "select a from SysOperate a join a.sysRoles b where a.id.menu=? and b.code=? order by a.orderNo";
        return find(hql, menuid, role);
    }

    @Override
    public boolean checkPermissionByRole(SysRoleOperate operateid, String role) {
        String hql = "select a from SysOperate a join a.sysRoles b where a.id.menu=? and a.id.op=? and b.code=?";
        SysOperate operate = (SysOperate) getObject(hql, operateid.getMenu(),
                operateid.getOp(), role);
        return operate != null;
    }
    @Override
    public JsonData getList(PagerTool pt,HttpServletRequest req) {
        StringBuffer cb = new StringBuffer("from SysOperate a where 1=1");
        StringBuffer lb = new StringBuffer("select new map(a.id.menu||'-'||a.id.op as id,a.id.op as  op,a.name as name,a.orderNo as ordno,a.url as url,a.icon as icon,a.isShow as isshow,a.sysMenu.name as menu) from SysOperate a where 1=1 ");
        Map<String, Object> params = RequestUtils.getQueryParams(req);;
        if (params.get("name") != null && params.get("name") != "" ){
            cb.append("  and a.name like '%"+params.get("name")+"%'");
            lb.append("  and a.name like '%"+params.get("name")+"%'");
        }if (params.get("op") != null && params.get("op") != "" ){
            cb.append("  and a.id.op like '%"+params.get("op")+"%'");
            lb.append("  and a.id.op like '%"+params.get("op")+"%'");
        }
        if (params.get("menu") != null && params.get("menu") != "" ){
            cb.append("  and a.sysMenu.name like '%"+params.get("menu")+"%'");
            lb.append("  and a.sysMenu.name like '%"+params.get("menu")+"%'");
        }if (params.get("url") != null && params.get("url") != "" ){
            cb.append("  and a.url like '%"+params.get("url")+"%'");
            lb.append("  and a.url like '%"+params.get("url")+"%'");
        }
        if (StringUtils.isNotEmpty(pt.getSortby())){
            lb.append("  order by "+pt.getSortby()+" "+pt.getSorttype());
        }
        JsonData jd = new JsonData();
        jd.setTotal(getCount(cb.toString()));
        jd.setRoot(find(pt.getStart(),pt.getEnd(),lb.toString()));
        return jd;
    }

    @Override
    public boolean checkPermission(String op, Long menu) {
        return false;
    }
}
